package com.xyz.rok.tools.service.impl;

import com.xyz.rok.tools.constant.Integral;
import com.xyz.rok.tools.constant.SystemCode;
import com.xyz.rok.tools.constant.Task;
import com.xyz.rok.tools.exception.UserException;
import com.xyz.rok.tools.mapper.CommandMapper;
import com.xyz.rok.tools.mapper.UnionMapper;
import com.xyz.rok.tools.mapper.UserMapper;
import com.xyz.rok.tools.model.Command;
import com.xyz.rok.tools.model.Union;
import com.xyz.rok.tools.service.CommandService;
import com.xyz.rok.tools.service.WxAppWrapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CommandServiceImpl implements CommandService {

    @Resource
    private CommandMapper commandMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UnionMapper unionMapper;
    @Autowired
    private WxAppWrapperService wxAppWrapperService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return commandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Command record) {
        return commandMapper.insert(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(Command record) throws Exception {
        if (record.getName().equals(Task.EXTRACT_UNION_TASK)) {
            Long unionId = Long.parseLong(record.getValue());
            Union union = unionMapper.selectByPrimaryKey(unionId);
            int rows = userMapper.updateIntegralAndIntegralLock(union.getUserId(), Integral.EXTRACT_UNION_TASK);
            if (rows == 0) {
                throw new UserException(SystemCode.USER_INTEGRAL_NOT_ENOUGH);
            }
            record.setCreateTime(new Date());
            wxAppWrapperService.pushMsg(1L, "有新的提取任务", new Date());
        }
        return commandMapper.insertSelective(record);
    }

    @Override
    public Command selectByPrimaryKey(Integer id) {
        return commandMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Command record) {
        return commandMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Command record) {
        return commandMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Command> selectUnExecutedCommand() {
        return commandMapper.selectUnExecutedCommand();
    }

    @Override
    public int updateBeginTime(Integer id) {
        return commandMapper.updateBeginTime(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateEndTime(Integer id) {
        Command record = commandMapper.selectByPrimaryKey(id);
        if (record.getName().equals(Task.EXTRACT_UNION_TASK)) {
            Long unionId = Long.parseLong(record.getValue());
            Union union = unionMapper.selectByPrimaryKey(unionId);
            userMapper.updateIntegralLock(union.getUserId(), Integral.EXTRACT_UNION_TASK);
            wxAppWrapperService.pushMsg(union.getUserId(), "联盟["+union.getName()+"]数据提取完成", new Date());
        }
        return commandMapper.updateEndTime(id);
    }

    @Override
    public Command getLastRunningStatus(Long unionId, String extractUnionTask) {
        return commandMapper.getLastRunningStatus(extractUnionTask, unionId.toString());
    }
}

